package controllers;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import models.Component;
import models.Event;
import models.Project;
import models.User;

import play.modules.search.Search;
import play.modules.search.Search.Query;
import play.mvc.Controller;
import play.mvc.With;

@With(Secure.class)
public class SearchEngine extends Controller {

	/**
	 * This method takes as a parameter
	 * a string and it searches for its
	 * occurrences in the projects in the system 
	 * @param value
	 * 				String to search for in the whole project
	 * @return
	 */
	public static List searchByProject(String value) {
		Query q = Search.search("name:" + value, Project.class);
		q.orderBy("name").page(2, 5).reverse();
		List<Project> projects = q.all().fetch();
		
		StringTokenizer st =  new StringTokenizer(value);
		while (st.hasMoreTokens()) {
			String desc1 = st.nextToken();
			String desc="";
			for(int i = 0; i < desc1.length();i++) {
				if(desc1.charAt(i)=='\'') {
					desc+="'";
				}
				desc+=desc1.charAt(i);
			}
			List<Project> searchDesc= Project.find("select p from Project p where p.description like '%"+ desc +"%'").fetch();
			for ( int i = 0 ; i < searchDesc.size() ; i ++)
				if  (! projects.contains(searchDesc.get(i)))
					projects.add(searchDesc.get(i));
		}
		return projects;
	}
	/**
	 * This method takes as a parameter
	 * a string and it searches for its
	 * occurrences in the users in the system 
	 * @param value
	 * 				String to search for in the whole project
	 * @return
	 */
	public static List searchByUser(String value) {
		List<User> searchUsers;
		Query q = Search.search("firstName:" + value, User.class);
		q.orderBy("firstName");
		List<User> users = q.all().fetch();
		searchUsers = users;
		q = Search.search("middleName:" + value, User.class);
		q.orderBy("middleName");
		users = q.all().fetch();
		for (int i = 0; i < users.size(); i++) {
			if (!searchUsers.contains(users.get(i)))
				searchUsers.add(users.get(i));
		}
		q = Search.search("lastName:" + value, User.class);
		q.orderBy("lastName");
		users = q.all().fetch();
		for (int i = 0; i < users.size(); i++) {
			if (!searchUsers.contains(users.get(i)))
				searchUsers.add(users.get(i));
		}
		q = Search.search("userName:" + value, User.class);
		q.orderBy("userName");
		users = q.all().fetch();
		for (int i = 0; i < users.size(); i++) {
			if (!searchUsers.contains(users.get(i)))
				searchUsers.add(users.get(i));
		}
		return searchUsers;
	}
	/**
	 * This method takes as a parameter
	 * a string and it searches for its
	 * occurrences in the events in the system 
	 * @param value
	 * 				String to search for in the whole project
	 * @return
	 */
	public static List searchByEvent(String value) {
		Query q = Search.search("name:" + value, Event.class);
		q.orderBy("name").page(2, 5).reverse();
		List<Event> events = q.all().fetch();

		
		StringTokenizer st =  new StringTokenizer(value);
		while (st.hasMoreTokens()) {
			String desc1 = st.nextToken();
			String desc="";
			for(int i = 0; i < desc1.length();i++) {
				if(desc1.charAt(i)=='\'') {
					desc+="'";
				}
				desc+=desc1.charAt(i);
			}
			List<Event> searchDesc= Event.find("select e from Event e where e.description like '%"+ desc +"%'").fetch();
			for ( int i = 0 ; i < searchDesc.size() ; i ++)
				if  (! events.contains(searchDesc.get(i)))
					events.add(searchDesc.get(i));
		}
		return events;
	}
	/**
	 * This method takes as a parameter
	 * a string and it searches for its
	 * occurrences in the components in the system 
	 * @param value
	 * 				String to search for in the whole project
	 * @return
	 */
	public static List searchByComponent(String value) {
		Query q = Search.search("name:" + value, Component.class);
		q.orderBy("name").page(2, 5).reverse();
		List<Component> components = q.all().fetch();

		StringTokenizer st =  new StringTokenizer(value);
		while (st.hasMoreTokens()) {
			String desc1 = st.nextToken();
			String desc="";
			for(int i = 0; i < desc1.length();i++) {
				if(desc1.charAt(i)=='\'') {
					desc+="'";
				}
				desc+=desc1.charAt(i);
			}
			List<Component> searchDesc= Component.find("select c from Component c where c.description like '%"+ desc +"%'").fetch();
			for ( int i = 0 ; i < searchDesc.size() ; i ++)
				if  (! components.contains(searchDesc.get(i)))
					components.add(searchDesc.get(i));
		}
		return components;
	}

	public static void search(String value) {
		List<Project> projects = searchByProject(value);
		List<User> users = searchByUser(value);
		List<Component> components = searchByComponent(value);
		List<Event> events = searchByEvent(value);
		long results =(users.size() + components.size() + projects.size() + events.size());
		render(projects, users, components, events, results);
	}
}